package gui.admin;

import gui.admin.mtm.PTM;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.Calendar;

public class ManageUsers extends JFrame {
    /*用户信息管理*/
    /**
     *
     *
     */
    /*关于日期的设定*/
    private static final String[] arrayYear = { "2024","2025" };
    private static final String[] arrayMonth = { "1", "2", "3", "4", "5", "6",
            "7", "8", "9", "10", "11", "12" };
    private static final String[] arrayDate = { "1", "2", "3", "4", "5", "6",
            "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17",
            "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28",
            "29", "30", "31" };


    private JComboBox<?> JCB_Year = new JComboBox<Object>(arrayYear);
    private JComboBox<?> JCB_Month = new JComboBox<Object>(arrayMonth);
    private JComboBox<?> JCB_Date = new JComboBox<Object>(arrayDate);
    JFrame jf = new JFrame();
    public ManageUsers() {


        //头部欢迎文字
        JPanel jp = new JPanel();
        jp.setBounds(160, 30, 500, 60);
        JLabel jL = new JLabel("欢迎使用用户管理系统！");
        //创建文字对象

        jL.setFont(new Font("Serif", Font.BOLD, 33));
        jL.setForeground(Color.darkGray);
        jp.add(jL);
//        jp.setBackground(Color.cyan);
        jf.add(jp);

        //日期设定
        JPanel upper = new JPanel();
        upper.setBounds(540, 90, 160, 40);
//        upper.setBackground(Color.cyan);
        JCB_Year.setToolTipText("年");
        upper.add(JCB_Year);
        JCB_Month.setToolTipText("月");
        upper.add(JCB_Month);
        JCB_Date.setToolTipText("日");
        upper.add(JCB_Date);

        JButton btn1 = new JButton("查询信息");
        JButton btn2 = new JButton("修改信息");
        JButton btn3 = new JButton("禁用用户");
        JButton btn4 = new JButton("删除用户");
        JPanel panel = new JPanel();



        //修改按钮
        btn2.setBounds(100, 400, 190, 30);
        jf.getContentPane().add(btn1);

        btn1.addActionListener(e -> {
            // TODO Auto-generated method stub
            System.out.println("查询按钮被点击了！");


        });


        //查询按钮
        btn1.setBounds(100, 100, 130, 30);

        btn2.addActionListener(e -> {
            System.out.println("修改按钮被点击了！");
        });
        jf.getContentPane().add(btn2);
        //手机号
        JLabel FilghtNumLabel = new JLabel("手机号");
        FilghtNumLabel.setBounds(250, 100, 100, 30);

        JTextField flightNum = new JTextField("输入手机号");


        //监听事件：
        flightNum.addFocusListener(new FocusListener() {

            @Override
            public void focusLost(FocusEvent e) {
                //失去焦点时，用户尚未在文本框输入任何内容，依旧显示提示文字
                if (flightNum.getText().equals("")) {
                    flightNum.setForeground(Color.gray);
                    flightNum.setText("输入手机号");

                }

            }

            @Override
            public void focusGained(FocusEvent e) {
                //得到焦点时，文本框的提示文字和创建该对象时的提示文字一样，说明用户正要输入内容
                if (flightNum.getText().equals("输入手机号")) {
                    flightNum.setText("");
                    flightNum.setForeground(Color.black);
                }

            }
        });

        flightNum.setBounds(310, 100, 150, 40);

        jf.getContentPane().add(FilghtNumLabel);
        jf.getContentPane().add(flightNum);

        //时间


        JLabel TimeLabel = new JLabel("注册时间");

        TimeLabel.setBounds(480, 100, 100, 30);
        jf.getContentPane().add(TimeLabel);

        // 设置开始当前日期
        Calendar calendar = Calendar.getInstance();
        java.util.Date date =  calendar.getTime();
        JCB_Year.setSelectedIndex(date.getYear() + 1900
                - Integer.parseInt(arrayYear[0]));
        JCB_Month.setSelectedIndex(date.getMonth());
        JCB_Date.setSelectedIndex(date.getDate() - 1);
        jf.add(upper);
        //修改按钮
        btn3.setBounds(300, 400, 190, 30);
        btn3.addActionListener(e -> {
            System.out.println("禁用用户按钮被点击了！");
        });
        jf.getContentPane().add(btn3);

        //删除按钮
        btn4.setBounds(500, 400, 190, 30);
        btn4.addActionListener(e -> {
            System.out.println("删除用户按钮被点击了！");
        });
        jf.getContentPane().add(btn4);



        //表格

        JTable jTable = new JTable(PTM.getInstance());
        JScrollPane scroll = new JScrollPane(jTable);

        scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        scroll.setPreferredSize(new Dimension(595, 220));
        jTable.setBackground(Color.white);
        jTable.setRowHeight(40);

        jTable.setGridColor(Color.pink);
        jTable.setSelectionBackground(Color.orange);
        //使表格文字居中
        DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();

        centerRenderer.setHorizontalAlignment(JLabel.CENTER);


        for (int i = 0; i < jTable.getColumnCount(); i++) {
            jTable.getColumnModel().getColumn(i).setCellRenderer(centerRenderer);

        }


        panel.setBorder(BorderFactory.createEmptyBorder(150, 0, 0, 0));

//        panel.setBackground(Color.cyan);

        panel.add(scroll);
        jf.getContentPane().add(panel);
        jf.setTitle("管理员—用户管理主界面");
        jf.setLocation(400, 300);
        jf.setSize(800, 500);
        jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        jf.setResizable(false);
        jf.setVisible(true);
    }



}
